//
//  MemoListController.m
//  MyNotes
//
//  Created by chen on 17/4/7.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "MemoListController.h"


#define CellIdentifier @"ListIdentifier"

@interface MemoListController ()
@property (strong,nonatomic) NSMutableArray *datalist;
@property (strong,nonatomic) Type *type;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *editButton;


@end

@implementation MemoListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"文件夹";
    TypeBL *bl = [TypeBL new];
    _datalist = bl.findAll;
    _editButton.title = @"Edit";
    _editButton.tag = 0;
    
    //广播通信
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadtableView:) name:@"GetTypeInformation" object:nil];
    
}

-(void)reloadtableView:(NSNotification *)notification{
    NSDictionary *dict = [notification userInfo];
    Type *type = [[Type alloc]init];
    type.tname = dict[@"fileName"];
    TypeBL *bl = [TypeBL new];
    //这里有可能出现buger
    _datalist = [bl createType:type];
    [self.tableView reloadData];
    
}
- (IBAction)editCell:(id)sender {
    if (_editButton.tag == 0) {
        [self.tableView setEditing:true animated:true];
        _editButton.title = @"Done";
        _editButton.tag = 1;
        [_editButton titleTextAttributesForState:UIControlStateNormal];
    }else{
        [self.tableView setEditing:false animated:true];
        _editButton.tag = 0;
        _editButton.title = @"Edit";
        [_editButton titleTextAttributesForState:UIControlStateNormal];
    }
}
- (IBAction)addAlertView:(id)sender {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"新建文件夹" message:@"请为此文件夹输入名称" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"名称";
        //验证输入文字是否为空
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"存储" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UITextField *fileName = alertController.textFields.firstObject;
        //传递数据
        NSLog(fileName.text);
        NSDictionary *dict = @{@"fileName":fileName.text};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetTypeInformation" object:nil userInfo:dict];
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:saveAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
//验证字符长度
- (void)alertTextFieldDidChange:(NSNotification *)notification{
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if (alertController) {
        UITextField *filename = alertController.textFields.firstObject;
        UIAlertAction *saveAction = alertController.actions.lastObject;
        saveAction.enabled = filename.text.length > 2;
    }
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _datalist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    _type = _datalist[indexPath.row];
    cell.textLabel.text = _type.tname;
    cell.detailTextLabel.text = _type.note1;
    return cell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        TypeBL *bl = [TypeBL new];
        [bl remove:_datalist[indexPath.row]];
        [_datalist removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }
    [self.tableView reloadData];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self performSegueWithIdentifier:@"MemoList" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"MemoList"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSInteger selectedIndex = [indexPath row];
        ViewController *viewController = segue.destinationViewController;
        Type *type = _datalist[selectedIndex];
        viewController.tid = type.tid;
        viewController.title = type.tname;
    }
}

@end
